home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / iref.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  16.7 KB  |  454 lines

  1. /* Copyright (C) 1989, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: iref.h,v 1.3 2000/09/19 19:00:46 lpd Exp $ */
  20. /* Object structure and type definitions for Ghostscript */
  21.  
  22. #ifndef iref_INCLUDED
  23. #  define iref_INCLUDED
  24.  
  25. /* The typedef for object references */
  26. #ifndef ref_DEFINED
  27. typedef struct ref_s ref;
  28. #  define ref_DEFINED
  29. #endif
  30.  
  31. /* The typedef for packed object references.  This is opaque here: */
  32. /* the details are in packed.h. */
  33. typedef ushort ref_packed;
  34.  
  35. #define log2_sizeof_ref_packed arch_log2_sizeof_short
  36. #define sizeof_ref_packed (1 << log2_sizeof_ref_packed)
  37.  
  38. /*
  39.  * Define the object types.
  40.  * The types marked with @ are composite and hence use the a_space field;
  41.  * objects of all other types must have a_space cleared.
  42.  * The types marked with ! behave differently in the interpreter
  43.  * depending on whether they are executable or literal.
  44.  * The types marked with + use the read/write/execute
  45.  * attributes; the rest only use the executable attribute.
  46.  * The types marked with # use the size field.
  47.  */
  48. typedef enum {
  49.  
  50. /*
  51.  * Type 0 must be left unassigned, so that the type (and type_attrs)
  52.  * of a valid ref will never be zero.  This speeds up simultaneous
  53.  * type/space checking in def (see dstack.h for details) and a similar
  54.  * check in ref_save (see store.h for details).  We may as well use
  55.  * type 0 for t__invalid, which will never appear in a real ref.
  56.  *
  57.  * The "invalid" type is only used in a few special places: the guard
  58.  * entries at the bottom of the o-stack that detect stack underflow,
  59.  * and (eventually) the ref that the cached value pointer in names points to
  60.  * if the binding isn't known.  It never appears on a stack or in a
  61.  * program-visible data structure.
  62.  */
  63.  
  64.     t__invalid,            /*      (no value) */
  65.     t_boolean,            /*      value.boolval */
  66.     t_dictionary,        /* @ +  value.pdict */
  67.     t_file,            /* @!+# value.pfile, uses size for id */
  68.  
  69. /*
  70.  * The 4 array types must be kept together, and must start at
  71.  * a multiple of 4, for the sake of r_is_array and r_is_proc (see below).
  72.  */
  73.  
  74. #define _t_array_span 4
  75.     t_array,            /* @!+# value.refs */
  76.     /* The following are the two implementations of */
  77.     /* packed arrays. */
  78.     t_mixedarray,        /* @!+# value.packed */
  79.     t_shortarray,        /* @!+# value.packed */
  80.     t_unused_array_,        /*      (an unused array type) */
  81.  
  82. /*
  83.  * t_[a]struct is an "umbrella" for other types that are represented by
  84.  * allocated objects (structures).  Objects of these types are composite
  85.  * and hence use the a_local attribute.  The type name is taken from
  86.  * the allocator template for the structure.  t_astruct objects use the
  87.  * access attributes; t_struct objects do not.  Neither t_struct nor
  88.  * t_astruct objects use the size.
  89.  *
  90.  * t_struct is currently used for the following PostScript types:
  91.  *      condition, lock.
  92.  * We could use it for fontIDs, except that they may have subclasses.
  93.  * Eventually it will also be used for the new 'device' type.
  94.  * t_astruct is currently used for the following PostScript types:
  95.  *      gstate.
  96.  *
  97.  * The 2 structure types must be kept together, and must start at
  98.  * a multiple of 2, for the sake of r_has_stype (see below).
  99.  */
  100.  
  101. #define _t_struct_span 2
  102.     t_struct,            /* @    value.pstruct */
  103.     t_astruct,            /* @ +  value.pstruct */
  104.  
  105. /*
  106.  * We now continue with individual types.
  107.  */
  108.     t_fontID,            /* @    value.pstruct */
  109.     t_integer,            /*      value.intval */
  110.     t_mark,            /*        (no value) */
  111. /*
  112.  * Name objects use the a_space field because they really are composite
  113.  * objects internally.
  114.  */
  115.     t_name,            /* @! # value.pname, uses size for index */
  116.     t_null,            /*  ! # (value.opproc, uses size for mark */
  117.     /*        type, on e-stack only) */
  118. /*
  119.  * Operator objects use the a_space field because they may actually be
  120.  * disguised procedures.  (Real operators always have a_space = 0.)
  121.  */
  122.     t_operator,            /* @! # value.opproc, uses size for index */
  123.     t_real,            /*      value.realval */
  124.     t_save,            /*      value.saveid, see isave.h for why */
  125.     /*        this isn't a t_struct */
  126.     t_string,            /* @!+# value.bytes */
  127. /*
  128.  * The following are extensions to the PostScript type set.
  129.  * If you add new types, be sure to edit:
  130.  *      - REF_TYPE_*STRINGS* and REF_TYPE_PROPERTIES_DATA below;
  131.  *      - the table in gs_init.ps (==only operator);
  132.  *      - the printing routine in idebug.c;
  133.  *      - the dispatches in igc.c, igcref.c, and interp.c;
  134.  *      - obj_cvs, obj_cvp, and obj_eq in iutil.c;
  135.  *      - restore_check_stack in zvmem.c.
  136.  */
  137.     t_device,            /* @ +   value.pdevice */
  138.     t_oparray,            /* @! #  value.const_refs, uses size */
  139.                 /*         for index */
  140.     t_next_index        /*** first available index ***/
  141. } ref_type;
  142.  
  143. /*
  144.  * The interpreter uses types starting at t_next_index for representing
  145.  * a few high-frequency operators.
  146.  * Since there are no operations specifically on operators,
  147.  * there is no need for any operators to check specifically for these
  148.  * types.  The r_btype macro takes care of the conversion when required.
  149.  */
  150. /*extern const int tx_next_index; *//* in interp.c */
  151. /*
  152.  * Define a table giving properties of types, similar to the table used
  153.  * by the isxxx functions (macros) in <ctype.h>.
  154.  */
  155. #define _rtype_uses_access 1    /* type uses w/r/x attributes */
  156. #define _rtype_uses_size 2
  157. #define _rtype_is_null 4
  158. #define _rtype_is_dictionary 8
  159. extern const byte ref_type_properties[1 << 6];    /* r_type_bits */
  160.  
  161. #define REF_TYPE_PROPERTIES_DATA\
  162.   0,                /* t__invalid */\
  163.   0,                /* t_boolean */\
  164.   _rtype_uses_access | _rtype_is_dictionary, /* t_dictionary */\
  165.   _rtype_uses_access | _rtype_uses_size, /* t_file */\
  166.   _rtype_uses_access | _rtype_uses_size, /* t_array */\
  167.   _rtype_uses_access | _rtype_uses_size, /* t_mixedarray */\
  168.   _rtype_uses_access | _rtype_uses_size, /* t_shortarray */\
  169.   _rtype_uses_access | _rtype_uses_size, /* (unused array type) */\
  170.   0,                /* t_struct */\
  171.   _rtype_uses_access,        /* t_astruct */\
  172.   0,                /* t_fontID */\
  173.   0,                /* t_integer */\
  174.   0,                /* t_mark */\
  175.   _rtype_uses_size,        /* t_name */\
  176.   _rtype_is_null,        /* t_null, uses size only on e-stack */\
  177.   _rtype_uses_size,        /* t_operator */\
  178.   0,                /* t_real */\
  179.   0,                /* t_save */\
  180.   _rtype_uses_access | _rtype_uses_size, /* t_string */\
  181.   _rtype_uses_access,        /* t_device */\
  182.   _rtype_uses_size,        /* t_oparray */\
  183.     /*\
  184.      * The remaining types are the extended pseudo-types used by the\
  185.      * interpreter for operators.  We need to fill up the table.\
  186.      */\
  187.   _rtype_uses_size,_rtype_uses_size,_rtype_uses_size, /*24*/\
  188.   _rtype_uses_size,_rtype_uses_size,_rtype_uses_size,_rtype_uses_size, /*28*/\
  189.   _rtype_uses_size,_rtype_uses_size,_rtype_uses_size,_rtype_uses_size, /*32*/\
  190.   _rtype_uses_size,_rtype_uses_size,_rtype_uses_size,_rtype_uses_size, /*36*/\
  191.   _rtype_uses_size,_rtype_uses_size,_rtype_uses_size,_rtype_uses_size, /*40*/\
  192.   _rtype_uses_size,_rtype_uses_size,_rtype_uses_size,_rtype_uses_size, /*44*/\
  193.   _rtype_uses_size,_rtype_uses_size,_rtype_uses_size,_rtype_uses_size, /*48*/\
  194.   _rtype_uses_size,_rtype_uses_size,_rtype_uses_size,_rtype_uses_size, /*52*/\
  195.   _rtype_uses_size,_rtype_uses_size,_rtype_uses_size,_rtype_uses_size, /*56*/\
  196.   _rtype_uses_size,_rtype_uses_size,_rtype_uses_size,_rtype_uses_size, /*60*/\
  197.   _rtype_uses_size,_rtype_uses_size,_rtype_uses_size,_rtype_uses_size    /*64 */
  198. #define _rtype_has(rtype,props)\
  199.   ((ref_type_properties[rtype] & (props)) != 0)
  200. #define ref_type_uses_access(rtype)\
  201.  _rtype_has(rtype, _rtype_uses_access)
  202. #define ref_type_uses_size(rtype)\
  203.  _rtype_has(rtype, _rtype_uses_size)
  204. #define ref_type_uses_size_or_null(rtype)\
  205.  _rtype_has(rtype, _rtype_uses_size | _rtype_is_null)
  206. /*
  207.  * Define the type names for debugging printout.
  208.  * All names must be the same length, so that columns will line up.
  209.  */
  210. #define REF_TYPE_DEBUG_PRINT_STRINGS\
  211.   "INVL","bool","dict","file",\
  212.   "arry","mpry","spry","u?ry",\
  213.   "STRC","ASTR",\
  214.   "font","int ","mark","name","null",\
  215.   "oper","real","save","str ",\
  216.   "devc","opry"
  217. /*
  218.  * Define the type names for the type operator.
  219.  */
  220. #define REF_TYPE_NAME_STRINGS\
  221.   0,"booleantype","dicttype","filetype",\
  222.   "arraytype","packedarraytype","packedarraytype","arraytype",\
  223.   0,0,\
  224.   "fonttype","integertype","marktype","nametype","nulltype",\
  225.   "operatortype","realtype","savetype","stringtype",\
  226.   "devicetype","operatortype"
  227. /*
  228.  * Define the type names for obj_cvp (the == operator).  We only need these
  229.  * for types that obj_cvp and obj_cvs don't handle specially.
  230.  */
  231. #define REF_TYPE_PRINT_STRINGS\
  232.   0,0,"-dict-","-file-",\
  233.   "-array-","-packedarray-","-packedarray-","-array-",\
  234.   0,0,\
  235.   "-fontID-",0,"-mark-",0,0,\
  236.   0,0,"-save-","-string-",\
  237.   "-device-",0
  238.  
  239. /*
  240.  * The following factors affect the encoding of attributes:
  241.  *
  242.  *      - The packed array format requires the high-order bits of the
  243.  *        type/attributes field to be 0.  (see packed.h)
  244.  *
  245.  *      - The interpreter wants the type, executable bit, and execute
  246.  *        permission to be adjacent, and in that order from high to low.
  247.  *
  248.  *      - Type testing is most efficient if the type is in a byte by itself.
  249.  *
  250.  * The layout given below results in the most efficient code overall.
  251.  */
  252.  
  253. /* Location attributes. */
  254. /* Note that these are associated with the *location*, not with the */
  255. /* ref that is *stored* in that location. */
  256. #define l_mark 1        /* mark for garbage collector */
  257. #define l_new 2            /* stored into since last save */
  258. /* Attributes visible at the PostScript language level. */
  259. /* Reserve bits for VM space information (defined in ivmspace.h). */
  260. #define r_space_bits 2
  261. #define r_space_shift 2
  262. #define a_write 0x10
  263. #define a_read 0x20
  264. #define a_execute 0x40
  265. #define a_executable 0x80
  266. #define a_readonly (a_read+a_execute)
  267. #define a_all (a_write+a_read+a_execute)
  268. #define r_type_shift 8
  269. #define r_type_bits 6
  270.  
  271. /* Define the attribute names for debugging printout. */
  272. /* Each entry has the form <mask, value, character>. */
  273. typedef struct attr_print_mask_s {
  274.     ushort mask;
  275.     ushort value;
  276.     char print;
  277. } attr_print_mask;
  278.  
  279. #define attr_print_flag(m,c)\
  280.   {m,m,c},{m,0,'-'}
  281. #define attr_print_space(v,c)\
  282.   {((1<<r_space_bits)-1)<<r_space_shift,v,c}
  283. #define attr_print_masks\
  284.   attr_print_flag(l_mark,'m'),\
  285.   attr_print_flag(l_new,'n'),\
  286.   attr_print_space(avm_foreign,'F'),\
  287.   attr_print_space(avm_system,'S'),\
  288.   attr_print_space(avm_global,'G'),\
  289.   attr_print_space(avm_local,'L'),\
  290.   attr_print_flag(a_write,'w'),\
  291.   attr_print_flag(a_read,'r'),\
  292.   attr_print_flag(a_execute,'x'),\
  293.   attr_print_flag(a_executable,'e'),\
  294.   attr_print_flag(0x4000,'?'),\
  295.   attr_print_flag(0x8000,'?')
  296.  
  297. /* Abstract types */
  298. typedef struct dict_s dict;
  299. typedef struct name_s name;
  300.  
  301. #ifndef stream_DEFINED
  302. #  define stream_DEFINED
  303. typedef struct stream_s stream;
  304. #endif
  305. #ifndef gx_device_DEFINED
  306. #  define gx_device_DEFINED
  307. typedef struct gx_device_s gx_device;
  308. #endif
  309. #ifndef obj_header_DEFINED
  310. #  define obj_header_DEFINED
  311. typedef struct obj_header_s obj_header_t;
  312. #endif
  313.  
  314. /*
  315.  * Define the argument type for operator procedures.  Note that the
  316.  * argument name is not arbitrary: it is used in access macros, so all
  317.  * operator procedures must use it.
  318.  */
  319. #ifndef i_ctx_t_DEFINED
  320. #  define i_ctx_t_DEFINED
  321. typedef struct gs_context_state_s i_ctx_t;
  322. #endif
  323. typedef int (*op_proc_t)(P1(i_ctx_t *i_ctx_p));
  324. /* real_opproc is a holdover.... */
  325. #define real_opproc(pref) ((pref)->value.opproc)
  326.  
  327. /* Object reference */
  328. /*
  329.  * Note that because of the way packed arrays are represented,
  330.  * the type_attrs member must be the first one in the ref structure.
  331.  */
  332. struct tas_s {
  333.     ushort type_attrs;
  334.     ushort rsize;
  335. };
  336. struct ref_s {
  337.  
  338.     struct tas_s tas;
  339.  
  340. #define r_size(rp) ((rp)->tas.rsize)
  341. #define r_inc_size(rp,inc) ((rp)->tas.rsize += (inc))
  342. #define r_dec_size(rp,dec) ((rp)->tas.rsize -= (dec))
  343. #define r_set_size(rp,siz) ((rp)->tas.rsize = (siz))
  344. /* type_attrs is a single element for fast dispatching in the interpreter */
  345. #if r_type_shift == 8
  346. #  if arch_is_big_endian
  347. #    define r_type(rp) (((const byte *)&((rp)->tas.type_attrs))[sizeof(ushort)-2])
  348. #  else
  349. #    define r_type(rp) (((const byte *)&((rp)->tas.type_attrs))[1])
  350. #  endif
  351. #  define r_has_type(rp,typ) (r_type(rp) == (typ))
  352. #else
  353. #  define r_type(rp) ((rp)->tas.type_attrs >> r_type_shift)
  354. #  define r_has_type(rp,typ) r_has_type_attrs(rp,typ,0)        /* see below */
  355. #endif
  356. /* A special macro for testing arrayhood. */
  357. #define r_is_array(rp) _r_has_masked_type_attrs(rp,t_array,_t_array_span,0)
  358. #define r_set_type(rp,typ) ((rp)->tas.type_attrs = (typ) << r_type_shift)
  359. #define r_btype(rp)\
  360.  ((rp)->tas.type_attrs >= (t_next_index << r_type_shift) ?\
  361.   t_operator : r_type(rp))
  362. #define r_type_xe_shift (r_type_shift - 2)
  363. #define type_xe_(tas) ((tas) >> r_type_xe_shift)    /* internal use only */
  364. /*
  365.  * The r_type_xe macro is used in (and only in) the main interpreter loop,
  366.  * where its rp operand may be a ref_packed, not necessarily aligned as
  367.  * strictly as a full-size ref.  The DEC C compiler, and possibly others,
  368.  * may compile code assuming that rp is ref-aligned.  Therefore, we
  369.  * explicitly cast the pointer to a less-strictly-aligned type.
  370.  * In order to convince the compiler, we have to do the cast before
  371.  * indexing into the structure.
  372.  */
  373. #define r_type_xe(rp)\
  374.   type_xe_(((const ushort *)(rp))[offset_of(ref, tas.type_attrs) / sizeof(ushort)])
  375. #define type_xe_value(t,xe) type_xe_(((t) << r_type_shift) + (xe))
  376. #define r_type_attrs(rp) ((rp)->tas.type_attrs)        /* reading only */
  377. #define r_has_attrs(rp,mask) !(~r_type_attrs(rp) & (mask))
  378. #define r_has_masked_attrs(rp,attrs,mask)\
  379.   ((r_type_attrs(rp) & (mask)) == (attrs))
  380. #define r_has_attr(rp,mask1)        /* optimize 1-bit case */\
  381.    (r_type_attrs(rp) & (mask1))
  382. /* The following macro is not for external use. */
  383. #define _r_has_masked_type_attrs(rp,typ,tspan,mask)\
  384.  (((rp)->tas.type_attrs &\
  385.    ((((1 << r_type_bits) - (tspan)) << r_type_shift) + (mask))) ==\
  386.   (((typ) << r_type_shift) + (mask)))
  387. #define r_has_type_attrs(rp,typ,mask)\
  388.   _r_has_masked_type_attrs(rp,typ,1,mask)
  389. /* A special macro for testing procedurehood. */
  390. #define r_is_proc(rp)\
  391.   _r_has_masked_type_attrs(rp,t_array,_t_array_span,a_execute+a_executable)
  392. #define r_set_attrs(rp,mask) ((rp)->tas.type_attrs |= (mask))
  393. #define r_clear_attrs(rp,mask) ((rp)->tas.type_attrs &= ~(mask))
  394. #define r_store_attrs(rp,mask,attrs)\
  395.   ((rp)->tas.type_attrs = ((rp)->tas.type_attrs & ~(mask)) | (attrs))
  396. #define r_copy_attrs(rp,mask,sp)\
  397.   r_store_attrs(rp,mask,(sp)->tas.type_attrs & (mask))
  398. #define r_set_type_attrs(rp,typ,mask)\
  399.   ((rp)->tas.type_attrs = ((typ) << r_type_shift) + (mask))
  400. /* Macros for t_[a]struct objects. */
  401. #define r_is_struct(rp) _r_has_masked_type_attrs(rp,t_struct,_t_struct_span,0)
  402. #define r_has_stype(rp,mem,styp)\
  403.   (r_is_struct(rp) && gs_object_type(mem, (rp)->value.pstruct) == &styp)
  404. #define r_ptr(rp,typ) ((typ *)((rp)->value.pstruct))
  405. #define r_set_ptr(rp,ptr) ((rp)->value.pstruct = (obj_header_t *)(ptr))
  406.  
  407.     union v {            /* name the union to keep gdb happy */
  408.     long intval;
  409.     ushort boolval;
  410.     float realval;
  411.     ulong saveid;
  412.     byte *bytes;
  413.     const byte *const_bytes;
  414.     ref *refs;
  415.     const ref *const_refs;
  416.     name *pname;
  417.     const name *const_pname;
  418.     dict *pdict;
  419.     const dict *const_pdict;
  420.     /*
  421.      * packed is the normal variant for referring to packed arrays,
  422.      * but we need a writable variant for memory management and for
  423.      * storing into packed dictionary key arrays.
  424.      */
  425.     const ref_packed *packed;
  426.     ref_packed *writable_packed;
  427.     op_proc_t opproc;
  428.     struct stream_s *pfile;
  429.     struct gx_device_s *pdevice;
  430.     obj_header_t *pstruct;
  431.     } value;
  432. };
  433.  
  434. /* Define data for initializing an empty array or string. */
  435. #define empty_ref_data(type, attrs)\
  436.   { /*tas*/ { /*type_attrs*/ ((type) << r_type_shift) | (attrs),\
  437.           /*rsize*/ 0 } }
  438.  
  439. /* Define the size of a ref. */
  440. #define arch_sizeof_ref sizeof(ref)
  441. /* Define the required alignment for refs. */
  442. /* We assume all alignment values are powers of 2. */
  443. #define arch_align_ref_mod\
  444.  (((arch_align_long_mod - 1) | (arch_align_float_mod - 1) |\
  445.    (arch_align_ptr_mod - 1)) + 1)
  446.  
  447. /* Define the maximum size of an array or a string. */
  448. /* The maximum array size is determined by the fact that */
  449. /* the allocator cannot allocate a block larger than max_uint. */
  450. #define max_array_size (max_ushort & (max_uint / (uint)arch_sizeof_ref))
  451. #define max_string_size max_ushort
  452.  
  453. #endif /* iref_INCLUDED */
  454.